package com.addplus.townmall.server.web.provider.serviceimpl.authoritymodule;

import com.addplus.townmall.server.api.constant.ErrorCode;
import com.addplus.townmall.server.api.exception.ErrorException;
import com.addplus.townmall.server.api.mapper.authority.RoleMapper;
import com.addplus.townmall.server.api.mapper.authority.RoleMenuElementMapper;
import com.addplus.townmall.server.api.mapper.authority.RoleMenuFunctionMapper;
import com.addplus.townmall.server.api.model.authority.Role;
import com.addplus.townmall.server.api.service.web.authoritymodule.RoleService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuElementMapper roleMenuElementMapper;

    @Autowired
    private RoleMenuFunctionMapper menufunctionMapper;


    @Override
    public Page<Role> getRoleByPage(Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo == null || pageSize == null) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Page<Role> page = new Page<Role>(pageNo,pageSize);
        Role role = new Role();
        role.setIsDelete(0);
        page= (Page<Role>) roleMapper.selectPage(page,new QueryWrapper<>(role));
        if (page != null && page.getTotal()!=0) {
            return page;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
    }

    @Override
    public Page<Role> getStoreRoleByPage(Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo == null || pageSize == null) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Page<Role> page = new Page<Role>(pageNo,pageSize);
        Role storeRole = new Role();
        storeRole.setIsDelete(0);
        page= (Page<Role>) roleMapper.selectPage(page,new QueryWrapper<Role>(storeRole));
        if (page != null && page.getTotal()!=0) {
            return page;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
    }

    @Override
    public Role getRoleById(Integer rId) throws Exception {
        boolean verify = DataUtils.EmptyOrNegativeOrZero(rId);
        if (verify) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Role role = new Role();
        role.setId(rId);
        role.setIsDelete(0);
        Role roleNew = roleMapper.selectOne(new QueryWrapper<Role>(role));
        if (roleNew != null) {
            return roleNew;
        }
        throw new ErrorException(ErrorCode.SYS_ERROR_NULLDATE);
    }

    @Override
    public Boolean addRole(Role role) throws Exception {
        if (role == null) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        role.setIsDelete(0);
        Date date = new Date();
        role.setGmtCreate(date);
        role.setGmtModified(date);
        int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
        role.setModifyUser(modifyUser);
        int count = roleMapper.insert(role);
        if (count > 0) {
            return true;
        } else {
            throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL, false);
        }
    }

    @Override
    public Boolean updateRoleById(Role role) throws Exception {
        if (role == null || DataUtils.EmptyOrNegative(role.getId())) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Role roleOld=roleMapper.selectById(role.getId());
        if(roleOld!=null){
            role.setGmtModified(new Date());
            int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
            role.setModifyUser(modifyUser);
            int count = roleMapper.updateById(role);
            if(count>0){
                return true;
            }else{
                throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL);
            }
        }else{
            throw new ErrorException(ErrorCode.SYS_ERROR_NOT_AVAILABLE);
        }
    }

    @Override
    @Transactional
    public Boolean logicallyDeleteRoleById(Integer rId) throws Exception{
        boolean verify = DataUtils.EmptyOrNegativeOrZero(rId);
        if (verify) {
            throw new ErrorException(ErrorCode.SYS_ERROR_PARAM);
        }
        Role role = new Role();
        role.setId(rId);
        role.setIsDelete(0);
        int count = roleMapper.selectCount(new QueryWrapper<Role>(role));
        if(count>0){
            //逻辑删除角色表里面色内容
            role.setIsDelete(1);
            int modifyUser= Integer.valueOf(RpcContext.getContext().getAttachment("id"));
            role.setModifyUser(modifyUser);
            count = roleMapper.updateById(role);
            if(count>0){
                //逻辑删除角色-菜单表内容
                menufunctionMapper.updateLogicallyDeleteByRoleId(rId);
                //逻辑删除角色-功能内容
//                roleMenuElementMapper.updateLogicallyDeleteByRoleId(rId);
                return true;
            }else{
                throw new ErrorException(ErrorCode.SYS_ERROR_DATABASEFAIL,false);
            }
        }else{
            throw new ErrorException(ErrorCode.SYS_ERROR_NOT_AVAILABLE,false);
        }
    }
}
